package com.hanamaki.iotest;

import java.io.*;

public class IoTest {

    void ioTest() throws IOException {
        System.out.println("数据写入");
        // 想要往系统磁盘中 存放文件 需要路径
        FileOutputStream fileOutputStream = new FileOutputStream("e:/a/text.txt");
        //定义数据
        byte[] content = {1, 2, 3, 4, 5, 6, 7};
        // 将数据写出
        fileOutputStream.write(content);
        // 将流关闭
        fileOutputStream.close();
        System.out.println("数据读出");
        FileInputStream fileInputStream = new FileInputStream("e:/a/text.txt");
        // 获取要读取的字节数
        int size = fileInputStream.available();
        for (int i = 0; i < size; i++) {
            System.out.print((char) fileInputStream.read() + "\t");
        }

    }


    void ioTestUtf8() throws IOException {
        // 文件路径
        File file = new File("e:/c/textUtf8.txt");
//        if (!file.exists()) {
//            file.mkdir();
//        }
        System.out.println(file.getName());
        System.out.println(file.getPath());

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
        outputStreamWriter.append("我是中国人");
        outputStreamWriter.append("他是English人");

        outputStreamWriter.close();
        fileOutputStream.close();


        System.out.println("读取文件内容");
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File("e:/c/textUtf8.txt")), "UTF-8");

        StringBuffer buffer = new StringBuffer();
        while (inputStreamReader.ready()) {
            buffer.append((char) inputStreamReader.read());
        }

        System.out.println(buffer.toString());

        inputStreamReader.close();

    }

    public static void main(String[] args) throws IOException {

        IoTest ioTest = new IoTest();
//        ioTest.ioTest();
        ioTest.ioTestUtf8();
    }
}